package org.beanstech.web;

import java.io.Writer;
import java.util.List;

import org.beanstech.dao.DropDownConstructDao;
import org.beanstech.model.SystemModel;

import com.sun.org.apache.bcel.internal.generic.GETSTATIC;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;
import com.thoughtworks.xstream.io.json.JsonWriter;

public class AjaxRequestHandler {

	public static String getJSONRequest(String q, String entity){
		
		DropDownConstructDao construct = new DropDownConstructDao();
		List<SystemModel> model =  null;
		XStream xstream = new XStream(new JsonHierarchicalStreamDriver(){
			 public HierarchicalStreamWriter createWriter(Writer writer) {
	                return new JsonWriter(writer,JsonWriter.DROP_ROOT_MODE);
	            }
		});
		try{
		
		
		 
		if(entity.equals("state"))	
			model = construct.getStateListforaCountry(Integer.parseInt(q));
		else if(entity.equals("city"))
			model = construct.getCityListfromStateId(Integer.parseInt(q));
		else if(entity.equals("college"))
			model = construct.getInstituitonFromUniversity(Integer.parseInt(q));
		else if(entity.equals("locality"))
			model = construct.getLocalityListfromCityid(Integer.parseInt(q));
		
		}catch(Exception e){
			e.printStackTrace();
		}
		 return xstream.toXML(model);
		
	}
	

	
	
}
